package com.example.mybatis.mapper;

import com.example.mybatis.dao.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    @Select("select id, username, password, age, gender, phone from userinfo")
    public List<UserInfo> queryAllUser();

    @Select("select username, password, age, gender, phone from userinfo where id = #{id}")
    public UserInfo queryById(Integer id);

    @Select("select username, password, age, gender, phone from userinfo where id = #{userid}")
    public UserInfo queryById2(@Param("userid")Integer id, String name);

    @Insert("insert into userinfo (username, password, age, gender, phone) " +
            "values(#{username}, #{password}, #{age}, #{gender}, #{phone})")
    public Integer insertSingle(UserInfo userInfo);

    @Options(useGeneratedKeys = true, keyProperty = "user.id")
    @Insert("insert into userinfo (username, password, age, gender, phone) " +
            "values(#{user.username}, #{user.password}, #{user.age}, #{user.gender}, #{user.phone})")
    public Integer insertSingle2(@Param("user")UserInfo userInfo, String s);

    @Delete("delete from userinfo where id >= #{id}")
    public void delete(Integer id);

    @Update("update userinfo set username = #{userInfo.username} where id = #{id}")
    public void update(UserInfo userInfo, Integer id);

    //查询表中所有属性
    //1.起别名
    @Select("select id, username, password, age, gender, phone, " +
            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime " +
            "from userinfo ")
    public List<UserInfo> selectAll();

    //2.结果映射
    @Select("select id, username, password, age, gender, phone," +
            "delete_flag, create_time, update_time from userinfo")
    @Results(id = "resultMap", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    public List<UserInfo> selectAll2();

   //3.开启驼峰命名
    @Select("select id, username, password, age, gender, phone," +
            "delete_flag, create_time, update_time from userinfo")
    public List<UserInfo> selectAll3();
}


